package com.yuanduo_app.bridge;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import cn.ydxlm.paylib.consts.PayType;
import com.xujl.fastlib.base.BaseModule;

import com.yuanduo_app.event.PayEvent;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;

public class PayModule extends BaseModule {


    public PayModule (@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @ReactMethod
    public void aliPay (String orderInfo) {
        EventBus.getDefault().post(new PayEvent(PayType.TYPE_ALI).putData("orderInfo", orderInfo));
    }

    @ReactMethod
    public void wxPay (ReadableMap map) {
        EventBus.getDefault().post(new PayEvent(PayType.TYPE_WX)
                .putData("partnerid", getStringValue("partnerid", map))
                .putData("prepayid", getStringValue("prepayid", map))
                .putData("noncestr", getStringValue("noncestr", map))
                .putData("timestamp", getStringValue("timestamp", map))
                .putData("sign", getStringValue("sign", map))
        );
    }

    private String getStringValue (String key, ReadableMap map) {
        if (!map.hasKey(key)) {
            return "";
        }
        return map.getString(key);
    }

    @NonNull
    @Override
    public String getName () {
        return "PayModule";
    }


}
